﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility.Peripheral;
using DevTeam;

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class KartelaGoster : XForm
    {
        public KartelaGoster()
        {
            InitializeComponent();
        }

        private void KartelaGoster_Load(object sender, EventArgs e)
        {
            

            gridView1.Init("GetKatalogPhotos");
            gridView1.SetCaptions("KatalogLinesPartId:#");
            gridView1.AddSummary();
            gridView1.BestFitColumns();
            gridView1.Columns["BLOB"].Width =
            gridView1.Columns["BLOBKod"].Width = 256;

            bbiKaydet.ItemClick += bbiKaydet_ItemClick;
            bbiExtract.ItemClick += bbiExtract_ItemClick;
            bbiExtract.Visibility =ytk(115) ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
        }

        void bbiExtract_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;

                int KatalogLinesPartId = 0;
                while (KatalogLinesPartId > -1)
                {
                    DataTable dt = ExecuteDataTable("GetKatalogLinesPartPhotos", KatalogLinesPartId);
                    if (dt.Rows.Count > 0)
                    {
                        KatalogLinesPartId = dt.Compute("MAX(KatalogLinesPartId)", string.Empty).ToInt();

                        foreach (DataRow r in dt.Rows)
                            if (r["BLOB"] != null)
                            {
                                int ProductId = r["ProductId"].ToInt();
                                Image scanned = ConvertByteArrayToImage(r["BLOB"] as byte[]);
                                //MemoryStream stream = new MemoryStream(r["BLOB"] as byte[]);

                                DataRow dr = photosTable.AddRow(ProductId, Combine(scanned), false);
                            }

                        ExecuteNonQuery("UpdPhoto7", photosTable.Table, Environment.MachineName);

                        GC.Collect();

                        photosTable = new Scanner.PhotosTable();
                    }
                    else
                        KatalogLinesPartId = -1;
                }
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            ExecuteNonQuery("UpdKatalogLinesPartPhotos", gridView1.SerializeChanges());
            gridView1.BestFitColumns();
            gridView1.Columns["BLOB"].Width =
            gridView1.Columns["BLOBKod"].Width = 256;
        }

        Image Combine(Image newImage)
        {
            Bitmap finalImage = new System.Drawing.Bitmap(1200, 1200);
            try
            {
                using (Graphics grfx = Graphics.FromImage(finalImage))
                {
                    //grfx.DrawImage((Image)Resources.transparent1200, 0, 0);
                    grfx.Clear(System.Drawing.Color.White);
                    grfx.DrawImage(newImage, 600 - newImage.Width / 2, 600 - newImage.Height / 2);
                    //grfx.DrawRectangle(new Pen(Color.Black, 2), 600 - newImage.Width / 2 - 1, 600 - newImage.Height / 2 - 1, newImage.Width + 2, newImage.Height + 2);

                    grfx.DrawString("*karteladan", new Font("MS Sans Serif", 22), new SolidBrush(Color.Black), new PointF(450 + newImage.Width / 2, 604 + newImage.Height / 2));
                    return finalImage;
                }
            }
            catch
            {
                if (finalImage != null)
                    finalImage.Dispose();
            }
            return null;
        }

        Scanner.PhotosTable photosTable = new Scanner.PhotosTable();

        //void UpdPhoto6(params object[] p)
        //{
        //    using (SqlConnection sqlConn = new SqlConnection(Session.DataManager.ConnectionString))
        //    {
        //        sqlConn.Open();
        //        SqlCommand sqlCmd = sqlConn.CreateCommand();
        //        sqlCmd.CommandText = "UpdPhoto7";
        //        sqlCmd.CommandType = CommandType.StoredProcedure;
        //        SqlParameter[] parameters = new SqlParameter[3];
        //        parameters[0] = new SqlParameter("@tbl", SqlDbType.Structured);
        //        parameters[0].Value = p[0];
        //        parameters[1] = new SqlParameter("@DeviceID", SqlDbType.VarChar);
        //        parameters[1].Value = p[1];
        //        parameters[2] = new SqlParameter("@TempScanInfo", SqlDbType.Int);
        //        parameters[2].Value = 0; // 0: "NoTempScan"  , 1: "GeciciTara"
        //        sqlCmd.Parameters.AddRange(parameters);
        //        sqlCmd.ExecuteNonQuery();
        //    }
        //}

    }
}
